package com.smyh.protcol;

import android.os.Looper;
import android.widget.Toast;

import com.smyh.UIM.MainActivity;
import com.smyh.UIM.TerminalParam;
import com.smyh.UIM.device.DeviceParam01Fm;
import com.smyh.UIM.device.DeviceParam02Fm;
import com.smyh.UIM.device.DeviceParam03Fm;
import com.smyh.UIM.device.DeviceParam04Fm;
import com.smyh.UIM.device.DeviceParam05Fm;
import com.smyh.UIM.device.DeviceParam06Fm;
import com.smyh.UIM.device.DeviceStateFragment;
import com.smyh.bean.DeviceMockInfo;
import com.smyh.bean.DeviceParam1;
import com.smyh.bean.DeviceParam2;
import com.smyh.bean.DeviceParam3;
import com.smyh.bean.DeviceParam4;
import com.smyh.bean.DeviceParam5;
import com.smyh.bean.DeviceParam6;
import com.smyh.bean.DigitDeviceInfo;
import com.smyh.bean.RFDeviceInfo;
import com.smyh.cache.DeviceDigitalInfoCache;
import com.smyh.cache.DeviceMockInfoCache;
import com.smyh.cache.DeviceParamInfoCache;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.util.AddCrc;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

public class ProtcolAnaly {

	public static void analyMsg(IoSession session,IoBuffer buffer){
		Object obj = session.getAttribute("LAST_BUF");
		IoBuffer totalBuf = IoBuffer.allocate(0).setAutoExpand(true);
		/*
		 * 把上次session数据和并到本次数据
		 */
		if(obj != null){
			totalBuf.put((IoBuffer)obj);
			session.removeAttribute("LAST_BUF");
		}
		totalBuf.put(buffer).flip();
		if ( totalBuf.getHexDump().contains("55 AA 08 D2 09 00 E3 16")) {
			MainCahce.on_offLineAllRF();
		}
		for(int i=0;i<totalBuf.limit();i++){
			if(totalBuf.limit()<i+7){
				if(totalBuf.limit()>=i+2){
					
					byte sureFram0 = totalBuf.get(i);
					byte sureFram1 = totalBuf.get(i+1);
					if(sureFram0 == ProtcolStruts.SURE_MSG_SUC[0]&&
							sureFram1 == ProtcolStruts.SURE_MSG_SUC[1]){
						System.out.println("确认回复!");
						i += 2-1;
					}
					if(sureFram0 == ProtcolStruts.SURE_MSG_CRCFAIL[0]&&
							sureFram1 == ProtcolStruts.SURE_MSG_CRCFAIL[1]){
						System.out.println("确认回复!");
						i += 2-1;
					}
				}
				IoBuffer leaveBuf = IoBuffer.allocate(0).setAutoExpand(true);
				leaveBuf.put(totalBuf.getSlice(i, totalBuf.limit()-i)).flip();
				session.setAttribute("LAST_BUF", leaveBuf);
				System.out.println("数据长度判断完!");
				return;
			}
			int head0 = totalBuf.get(i)&0xff;
			int head1 = totalBuf.get(i+1)&0xff;
			
			if(0x55 == head0 && 0xaa == head1){
				int length = totalBuf.get(i+2)&0xff;
				if(length + i <= totalBuf.limit()){
					byte crcSum = totalBuf.get(i+length-2);
					//校验码是除去 ----报文头----尾----的其他字节和
					byte countCrc = AddCrc.getCrc(totalBuf, i+2, i+length-3);
					if(crcSum == countCrc){
						IoBuffer replyBuf = IoBuffer.allocate(0).setAutoExpand(true);
						replyBuf.put((byte) 0xff).put(ProtcolStruts.SURE_MSG_SUC).flip();
						//短帧确认应答
						session.write(replyBuf);
						
						//报文类型判定：
						byte type0 = totalBuf.get(i+3);
						byte type1 = totalBuf.get(i+4);
						//本地参数查询应答
						if(type0 == ProtcolStruts.OPTION_ANSWER_SUCC[0] ){
							//物理地址：
//							if(UIElement.messDialog==null){
//								UIElement.messDialog = new MessageDialog(
//										new javax.swing.JFrame(), false);
//							}
							
							if(type1 == ProtcolStruts.OPTION_ANSWER_SUCC[1]){
								// TODO: 2016/7/19
								Looper.prepare();
								Toast.makeText(MainActivity.context,"操作成功！",Toast.LENGTH_SHORT).show();
								Looper.loop();
							}else{
								Looper.prepare();
								Toast.makeText(MainActivity.context,"操作失败！",Toast.LENGTH_SHORT).show();
								Looper.loop();
							}
							
							i += length -1;
							continue;
						} 
						

						//终端参数参数查询应答
						if(type0 == ProtcolStruts.DEVICE_ANSWER_CMD[0] &&
								type1 == ProtcolStruts.DEVICE_ANSWER_CMD[1]){
							//物理地址：
							String addr = totalBuf.duplicate().getSlice(i+5, 4).getHexDump();
							if(!"00 00 00 00".equals(addr)){
								RFDeviceInfo device = MainCahce.deviceInfos.get(addr);
								if(device == null){
									device = new RFDeviceInfo();
									device.setAddr(addr);
								}
								int subType = totalBuf.get(i+10);
								if(subType == 3){
									byte[] groupAddr = {totalBuf.get(i+10+2),
											totalBuf.get(i+10+3),totalBuf.get(i+10+5)};
									byte linePhaseOff = totalBuf.get(i+10+6);
									device.setGroupAddr(groupAddr);
									device.setLinePhaseOff(linePhaseOff);
								}
								String content = totalBuf.duplicate().getSlice(i+10, 20).getHexDump();
								device.setContent(subType, content);
								TerminalParam.refreshDeviceList(addr);
//								if(subType<8){
//									IoBuffer sendBuf = CMDUtil.askLocalParam(HexStrUtil.hexStr2Bytes(addr), (byte) (subType+1));
//									session.write(sendBuf);
//								}
								i += length-1;
								continue;
							}
						}
						
						/**
						 * 指示器遥信、遥测报文数据解析
						 */
						if(type0 == ProtcolStruts.CHANGE_DIGIT_MOCK_ANSWER_CMD[0] &&
								type1 == ProtcolStruts.CHANGE_DIGIT_MOCK_ANSWER_CMD[1]){
							/**
							 * 数据长度：n*29+8-n:指示器数据数目
							 */
							int counts = (length-8)/29;
							boolean hasWarn = false;
							for(int j=0;j<counts;j++){
								
								IoBuffer bufContentTemp = totalBuf.duplicate().getSlice(i+6+j*29, 29);
								//物理地址：
								String addr = bufContentTemp.duplicate().getSlice(0, 4).getHexDump();
								if(!"00 00 00 00".equals(addr)){
									// TODO: 2016/7/19
									DeviceMockInfo info = DeviceMockInfoCache.getDeviceMockMap().get(addr);
									if(info == null){
										info = new DeviceMockInfo();
										info.setRFAddr(addr);
										DeviceMockInfoCache.getDeviceMockMap().put(addr, info);
									}
									int elecVaule = bufContentTemp.getShort(10)&0xffff;
									int batteryVolt = bufContentTemp.getShort(12)&0xffff;
									int faultElecVaule = bufContentTemp.getShort(14)&0xffff;
									int temper = bufContentTemp.get(16)&0xff;
									int elecField = bufContentTemp.getShort(17)&0xffff;
									int elecCapacityV = bufContentTemp.getShort(19)&0xffff;
									int state = bufContentTemp.get(21)&0xff;
									int fault = bufContentTemp.get(22)&0xff;
									// TODO: 2016/7/19
									info.setLoadElec(elecVaule);
									info.setBatteryElecVolt(batteryVolt);
									info.setFaultElec(faultElecVaule);
									info.setCableTemper(temper-50);
									info.setGroupField(elecField);
									info.setCapacitanceElecVolt(elecCapacityV);
									info.setStateInfo(state);
									info.setFaultInfo(fault);
									byte stateByte = (byte) state;
									byte faultByte = (byte) fault;
									// TODO: 2016/7/19
									//判断短路
									if((faultByte&0x80) == (0x80&0xff)){
										hasWarn = true;
										DigitDeviceInfo digitInfo = DeviceDigitalInfoCache.getDeviceDigitMap().get(addr);
										if(digitInfo == null){
											digitInfo = new DigitDeviceInfo();
											digitInfo.setRFAddr(addr);
											DeviceDigitalInfoCache.getDeviceDigitMap().put(addr, digitInfo);
										}
										digitInfo.setShortFault0(faultElecVaule);
										digitInfo.setShortFaultCurrentTime(System.currentTimeMillis());
									}
									//判断接地
									if((faultByte&0x40) == (0x40&0xff)){
										hasWarn = true;
										DigitDeviceInfo digitInfo = DeviceDigitalInfoCache.getDeviceDigitMap().get(addr);
										if(digitInfo == null){
											digitInfo = new DigitDeviceInfo();
											digitInfo.setRFAddr(addr);
											DeviceDigitalInfoCache.getDeviceDigitMap().put(addr, digitInfo);
										}
										digitInfo.setEarthFault0(faultElecVaule);
										digitInfo.setEarthFaultCurrentTime(System.currentTimeMillis());
									}
									//电池报警
									if((faultByte&0x8) == 8){
										hasWarn = true;
										DigitDeviceInfo digitInfo = DeviceDigitalInfoCache.getDeviceDigitMap().get(addr);
										if(digitInfo == null){
											digitInfo = new DigitDeviceInfo();
											digitInfo.setRFAddr(addr);
											DeviceDigitalInfoCache.getDeviceDigitMap().put(addr, digitInfo);
										}
										digitInfo.setBatteryFault0(batteryVolt);
										digitInfo.setBatteryFaultCurrentTime(System.currentTimeMillis());
									}
									
								}
							}
							if(hasWarn){
								//DeviceDigitModel.freshDigitDataModel();
							}
							DeviceStateFragment.freshMockState();
							//DeviceMockModel.freshMockDataModel();
							i += length-1;
							continue;
						}
						
						//外设参数查询应答
						if(type0 == ProtcolStruts.OUTER_ANSWER_CMD[0] &&
								type1 == ProtcolStruts.OUTER_ANSWER_CMD[1]){
							//物理地址：
							String addr = totalBuf.duplicate().getSlice(i+5, 4).getHexDump();
							if(!"00 00 00 00".equals(addr)){
								int subType = totalBuf.get(i+10)&0xff;
								switch (subType) {
								case 1:{
									DeviceParam1 param = DeviceParamInfoCache.getDeviceParamMap1().get(addr);
									if(param == null){
										param = new DeviceParam1();
										param.setRFAddr(addr);
										DeviceParamInfoCache.getDeviceParamMap1().put(addr, param);
									}
									int hasElecVolt = totalBuf.getShort(i+11+0)&0xffff;
									int hasElecELec = totalBuf.getShort(i+11+2)&0xffff;
									int inSmallElec = totalBuf.getShort(i+11+4)&0xffff;
									int inBigElec = totalBuf.getShort(i+11+6)&0xffff;
									int earthIntervalTime = totalBuf.get(i+11+8)&0xff;
									int earthPulsCounts = totalBuf.get(i+11+9)&0xff;
									int shortSudValue = totalBuf.getShort(i+11+10)&0xffff;
									int shortOverProtectValue = totalBuf.getShort(i+11+12)&0xffff;
									int overProtectMinValue = totalBuf.getShort(i+11+14)&0xffff;
									int overProtectMaxValue = totalBuf.getShort(i+11+16)&0xffff;
									param.setHasElecVolt(hasElecVolt);
									param.setHasElecElec(hasElecELec);
									param.setInSmallElec(inSmallElec);
									param.setInBigElec(inBigElec);
									param.setEarthIntervalTime(earthIntervalTime);
									param.setEarthPulsCounts(earthPulsCounts);
									param.setShortSudValue(shortSudValue);
									param.setShortOverProtectValue(shortOverProtectValue);
									param.setOverProtectMinValue(overProtectMinValue);
									param.setOverProtectMaxValue(overProtectMaxValue);
									DeviceParam01Fm.freshDeviceParam1Model();
									break;
								}
									
								case 2:{
									// TODO: 2016/7/19
									DeviceParam2 param = DeviceParamInfoCache.getDeviceParamMap2().get(addr);
									if(param == null){
										param = new DeviceParam2();
										param.setRFAddr(addr);
										DeviceParamInfoCache.getDeviceParamMap2().put(addr, param);
									}
									int fastBreakProtoclMaxTime = totalBuf.getShort(i+11+0)&0xffff;
									int hasElecTime = totalBuf.getShort(i+11+2)&0xffff;
									int stopElecTime = totalBuf.getShort(i+11+4)&0xffff;
									int resetTime = totalBuf.getShort(i+11+6)&0xffff;
									int mockSendupIntervalTime = totalBuf.getShort(i+11+8)&0xffff;
									int hasElecResetAble = totalBuf.get(i+11+10)&0xff;
									int hasElecResetTime = totalBuf.getShort(i+11+11)&0xffff;
									int mockDeadSendupAble = totalBuf.get(i+11+13)&0xff;
									int mockDeadFixValue = totalBuf.getShort(i+11+14)&0xffff;
									int temperGatherAble = totalBuf.get(i+11+16)&0xff;
									int temperChangWarnValue = totalBuf.get(i+11+17)&0xff;
									// TODO: 2016/7/19
									param.setFastBreakProtoclMaxTime(fastBreakProtoclMaxTime);
									param.setHasElecTime(hasElecTime);
									param.setStopElecTime(stopElecTime);
									param.setResetTime(resetTime);
									param.setMockSendupIntervalTime(mockSendupIntervalTime);
									param.setHasElecResetAble(hasElecResetAble);
									param.setHasElecResetTime(hasElecResetTime);
									param.setMockDeadSendupAble(mockDeadSendupAble);
									param.setMockDeadFixValue(mockDeadFixValue);
									param.setTemperGatherAble(temperGatherAble);
									param.setTemperChangWarnValue(temperChangWarnValue);

									DeviceParam02Fm.freshDeviceParam2Model();
									break;
								}
								
								case 3:{
									DeviceParam3 param = DeviceParamInfoCache.getDeviceParamMap3().get(addr);
									if(param == null){
										param = new DeviceParam3();
										param.setRFAddr(addr);
										DeviceParamInfoCache.getDeviceParamMap3().put(addr, param);
									}
									int batteryLowWarn = totalBuf.get(i+11+0)&0xff;
									int batteryLowWarnValue = totalBuf.getUnsignedShort(i+11+1);
									int defendErrorAction10MSValue = totalBuf.get(i+11+3)&0xff;
									int DMAChangeAllChanel = totalBuf.get(i+11+4)&0xff;
									int voltChangePersent = totalBuf.get(i+11+5)&0xff;
									int faultElecWaveResetTime = totalBuf.getUnsignedShort(i+11+6);
									int tempAble = totalBuf.get(i+11+8)&0xff;
									int tempHarryChange = totalBuf.getUnsignedShort(i+11+9);
									int tempVoltStartValue = totalBuf.getUnsignedShort(i+11+11);
									int recordElecBreakValue = totalBuf.getUnsignedShort(i+11+13);
									int recordElecFildStartValue = totalBuf.getUnsignedShort(i+11+15);
									int recordElecFildPercentValue = totalBuf.getUnsignedShort(i+11+17);

									param.setBatteryLowWarn(batteryLowWarn);
									param.setBatteryLowWarnValue(batteryLowWarnValue);
									param.setDefendErrorAction10MSValue(defendErrorAction10MSValue);
									param.setDMAChangeAllChanel(DMAChangeAllChanel);
									param.setVoltChangePersent(voltChangePersent);
									param.setFaultElecWaveResetTime(faultElecWaveResetTime);
									param.setTempAble(tempAble);
									param.setTempHarryChange(tempHarryChange);
									param.setTempVoltStartValue(tempVoltStartValue);
									param.setRecordElecBreakValue(recordElecBreakValue);
									param.setRecordElecFildStartValue(recordElecFildStartValue);
									param.setRecordElecFildPercentValue(recordElecFildPercentValue);
									
									DeviceParam03Fm.freshDeviceParam3Model();
									break;
								}
									
								case 4:{
									// TODO: 2016/7/19
									DeviceParam4 param = DeviceParamInfoCache.getDeviceParamMap4().get(addr);
									if(param == null){
										param = new DeviceParam4();
										param.setRFAddr(addr);
										DeviceParamInfoCache.getDeviceParamMap4().put(addr, param);
									}
									byte[] versionInfoBytes = {totalBuf.get(i+11),totalBuf.get(i+11+1),totalBuf.get(i+11+2),totalBuf.get(i+11+3),
											totalBuf.get(i+11+4),totalBuf.get(i+11+5),totalBuf.get(i+11+6),totalBuf.get(i+11+7)};
									String versionNum = new String(versionInfoBytes);
									int runTimes = (int)(totalBuf.get(i+11+8)&0xff) + (int)((totalBuf.get(i+11+9)&0xff)*256) + (int)((totalBuf.get(i+11+10)&0xff)*256*256) 
											+ (int)((totalBuf.get(i+11+11)&0xff)*256*256*256) ;
									int workStyle = totalBuf.get(i+11+12)&0xff;
									
									int reserv1 = totalBuf.get(i+11+13)&0xff;
									int reserv2 = totalBuf.get(i+11+14)&0xff;
									int reserv3 = totalBuf.get(i+11+15)&0xff;
									int reserv4 = totalBuf.get(i+11+16)&0xff;
									int reserv5 = totalBuf.get(i+11+17)&0xff;
									int reserv6 = totalBuf.get(i+11+18)&0xff;
									int reserv7 = totalBuf.get(i+11+19)&0xff;
									int reserv8 = totalBuf.get(i+11+20)&0xff;
									// TODO: 2016/7/19
									param.setVersionNum(versionNum);
									param.setRunTimes(runTimes);
									param.setWorkStyle(workStyle);
									param.setReserv1(reserv1);
									param.setReserv2(reserv2);
									param.setReserv3(reserv3);
									param.setReserv4(reserv4);
									param.setReserv5(reserv5);
									param.setReserv6(reserv6);
									param.setReserv7(reserv7);
									param.setReserv8(reserv8);

									DeviceParam04Fm.freshDeviceParam4Model();
									break;
								}
									
								case 5:{
									DeviceParam5 param = DeviceParamInfoCache.getDeviceParamMap5().get(addr);
									if(param == null){
										param = new DeviceParam5();
										param.setRFAddr(addr);
										DeviceParamInfoCache.getDeviceParamMap5().put(addr, param);
									}
									
									int mockOverlimitVarElec = (int)(totalBuf.get(i+11+0)&0xff) + (int)(totalBuf.get(i+11+1)&0xff)*256;
									int mockOverlimitVarPercent = totalBuf.get(i+11+2)&0xff;
									int mockReloadUpElec = (int)(totalBuf.get(i+11+3)&0xff) + (int)(totalBuf.get(i+11+4)&0xff)*256;
									int allWorkElec = (int)(totalBuf.get(i+11+5)&0xff) + (int)(totalBuf.get(i+11+6)&0xff)*256;
									int rfParamSet = (int)(totalBuf.get(i+11+7)&0xff) ;
									int midFuncSwitch = (int)(totalBuf.get(i+11+8)&0xff) ;

									param.setMockOverlimitVarElec(mockOverlimitVarElec);
									param.setMockOverlimitVarPercent(mockOverlimitVarPercent);
									param.setMockReloadUpElec(mockReloadUpElec);
									param.setAllWorkElec(allWorkElec);
									param.setRFParamSet(rfParamSet);
									param.setMidFnSwitch(midFuncSwitch);

									DeviceParam05Fm.freshDeviceParam5Model();
									break;
								}
								case 6:{

									DeviceParam6 param = DeviceParamInfoCache.getDeviceParamMap6().get(addr);
									if(param == null){
										param = new DeviceParam6();
										param.setRFAddr(addr);
										DeviceParamInfoCache.getDeviceParamMap6().put(addr, param);
									}

									int waveSwitch = totalBuf.getUnsigned(i+11+0);
									int elecDCBranch = totalBuf.getUnsigned(i+11+1) + (totalBuf.getUnsigned(i+11+2))*256;
									int voltDCBranch = totalBuf.getUnsigned(i+11+3) + (totalBuf.getUnsigned(i+11+4))*256;
									int beforeFaultWaveCount = totalBuf.getUnsigned(i+11+5);
									int afterFaultWaveCount = totalBuf.getUnsigned(i+11+6);
									int smapleCycleCheck = totalBuf.getUnsigned(i+11+7) + (totalBuf.getUnsigned(i+11+8))*256;

									int elecRatio_1 = totalBuf.getUnsigned(i+11+9) + (totalBuf.getUnsigned(i+11+10))*256;
									int elecRatio_2 = totalBuf.getUnsigned(i+11+11) + (totalBuf.getUnsigned(i+11+12))*256;
									int voltRatio1 = totalBuf.getUnsigned(i+11+13) + (totalBuf.getUnsigned(i+11+14))*256;
									int voltRatio2 = totalBuf.getUnsigned(i+11+15) + (totalBuf.getUnsigned(i+11+16))*256;

									param.setWaveSwitch(waveSwitch);
									param.setElecDCBranch(elecDCBranch);
									param.setVoltDCBranch(voltDCBranch);
									param.setBeforeFaultWaveCount(beforeFaultWaveCount);
									param.setAfterFaultWaveCount(afterFaultWaveCount);
									param.setSmapleCycleCheck(smapleCycleCheck);
									param.setElecRatio_1(elecRatio_1);
									param.setElecRatio_2(elecRatio_2);
									param.setVoltRatio1(voltRatio1);
									param.setVoltRatio2(voltRatio2);

									DeviceParam06Fm.freshDeviceParam6Model();
									break;
								}
									
								default:
									break;
								}
								
							}
							i += length-1;
							continue;
						}
						
						//终端设备寻呼查询应答 设备上线
						if(type0 == ProtcolStruts.ALL_CALL_ANSWER_CMD[0] &&
								type1 == ProtcolStruts.ALL_CALL_ANSWER_CMD[1]){
							// TODO: 2016/7/19
							//物理地址：
							int contentLenth = totalBuf.get(i+5)&0xff;
							for(int j=0;j<(contentLenth/12);j++){
								String addr = totalBuf.duplicate().getSlice(i+j*12+6, 4).getHexDump();
								byte[] groupAddr = {totalBuf.get(i+j*12+6+4),
										totalBuf.get(i+j*12+6+4+1),totalBuf.get(i+j*12+6+4+3)};
								byte linePhaseOff = totalBuf.get(i+j*12+6+4+4);
								RFDeviceInfo device = new RFDeviceInfo();
								device.setAddr(addr);
								int deviceType = totalBuf.get(i+j*12+10+5);
								device.setDeviceType(deviceType);
								device.setStatus(1);
								device.setActionTime(System.currentTimeMillis());
								device.setGroupAddr(groupAddr);
								device.setLinePhaseOff(linePhaseOff);
								MainCahce.deviceInfos.put(addr,device);
							}
							MainActivity.refreshDeviceList();
							i += length-1;
							continue;
						}
						
						//设备掉线报文：8字节 55 AA 08 D2 09 00 E3 16 
						if(type0 == ProtcolStruts.WAIT_OFF_LIVE_CMD[0] &&
								type1 == ProtcolStruts.WAIT_OFF_LIVE_CMD[1]){
							// TODO: 2016/7/19
							//物理地址：
							/*RFDeviceModel.on_offLineAllRF(false);
							if(ContentMainUI_cache.connectButton != null){
								JButton button = ContentMainUI_cache.connectButton;
								
								if(ContentMainUI_cache.connectRFAddr != null){
									ContentMainUI_cache.connectRFAddr.setText("----");
								}
								button.setBackground(Color.RED);
								button.setText("建立连接");
								button.setSelected(false);
							}*/
							i += length-1;
							continue;
						}
						
						if(type0 == ProtcolStruts.OUTER_CONTROL_CMD[0] &&
								type1 == ProtcolStruts.OUTER_CONTROL_CMD[1]){
							// TODO: 2016/7/19

							boolean hasWarn = false;
							//物理地址：
							String addr =MainCahce.chooseDeviceAddr;
//							if(UIElement.isBatchAlterParam){
//								addr = UIElement.isBatchReadAddr.get(0);
//							}
							DeviceMockInfo info = DeviceMockInfoCache.getDeviceMockMap().get(addr);
							if(info == null){
								info = new DeviceMockInfo();
								info.setRFAddr(addr);
								DeviceMockInfoCache.getDeviceMockMap().put(addr, info);
							}
							if(!addr.equals("00 00 00 00")){
								int elecVaule = totalBuf.getShort(i+11)&0xffff;
								int batteryVolt = totalBuf.getShort(i+13)&0xffff;
								int faultElecVaule = totalBuf.getShort(i+15)&0xffff;
								int temper = totalBuf.get(i+17)&0xff;
								int elecField = totalBuf.getShort(i+18)&0xffff;
								int elecCapacityV = totalBuf.getShort(i+20)&0xffff;
								int state = totalBuf.get(i+22)&0xff;
								int fault = totalBuf.get(i+23)&0xff;

								info.setLoadElec(elecVaule);
								info.setBatteryElecVolt(batteryVolt);
								info.setFaultElec(faultElecVaule);
								info.setCableTemper(temper-50);
								info.setGroupField(elecField);
								info.setCapacitanceElecVolt(elecCapacityV);
								info.setStateInfo(state);
								info.setFaultInfo(fault);
								DeviceDigitalInfoCache.getDeviceDigitMap().remove(addr);
								byte faultByte = totalBuf.get(i+23);
								//判断短路
								if((faultByte&0x80) == (0x80&0xff)){
									hasWarn = true;
									DigitDeviceInfo digitInfo = DeviceDigitalInfoCache.getDeviceDigitMap().get(addr);
									if(digitInfo == null){
										digitInfo = new DigitDeviceInfo();
										digitInfo.setRFAddr(addr);
										DeviceDigitalInfoCache.getDeviceDigitMap().put(addr, digitInfo);
									}
									digitInfo.setShortFault0(faultElecVaule);
								}
								//判断接地
								if((faultByte&0x40) == (0x40&0xff)){
									hasWarn = true;
									DigitDeviceInfo digitInfo = DeviceDigitalInfoCache.getDeviceDigitMap().get(addr);
									if(digitInfo == null){
										digitInfo = new DigitDeviceInfo();
										digitInfo.setRFAddr(addr);
										DeviceDigitalInfoCache.getDeviceDigitMap().put(addr, digitInfo);
									}
									digitInfo.setEarthFault0(faultElecVaule);
								}
								//电池报警
								if((faultByte&0x8) == 8){
									hasWarn = true;
									DigitDeviceInfo digitInfo = DeviceDigitalInfoCache.getDeviceDigitMap().get(addr);
									if(digitInfo == null){
										digitInfo = new DigitDeviceInfo();
										digitInfo.setRFAddr(addr);
										DeviceDigitalInfoCache.getDeviceDigitMap().put(addr, digitInfo);
									}
									digitInfo.setBatteryFault0(batteryVolt);
								}
								if(hasWarn){
								}
								DeviceStateFragment.freshMockState();

							}
							
						}

					}else{
						System.out.println("crc校验错误!");
					}
				}else{
					IoBuffer leaveBuf = IoBuffer.allocate(0).setAutoExpand(true);
					leaveBuf.put(totalBuf.getSlice(i, totalBuf.limit()-i)).flip();
					session.setAttribute("LAST_BUF", leaveBuf);
					System.out.println("报文长度不对,格式长度：" + length + "; 实际报文："+ totalBuf.limit());
					
					return;
				}
			}
		}
		
	}
}
